﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;

namespace CMTVision.Widgets.Framework
{
    [System.Web.UI.ViewStateModeById]
    public class HtmlDivTemplateColumn:System.Web.UI.Control,ITemplateColumn
    {
        #region Ctors

        public HtmlDivTemplateColumn()
            : base()
        {
            LockMode = ColumnWidgetLockMode.None;
        }
         
        #endregion

        #region Implement ITemplateColumn

        public long TemplateID
        {
            get;
            set;
        }

        public string ColumnID
        {
            get;
            set;
        }

        public bool IsInEditMode
        {
            get;
            set;
        }
         
        public System.Web.UI.Control ContainersHolder
        {
            get
            { 
                return this;
            }
        }

        public ColumnWidgetLockMode LockMode
        {
            get;
            set;
        }

        public void LoadWidgets()
        {
            Controls.Clear();
            List<Model.Widget> allWidgets = new List<Model.Widget>();
            using (Model.CMTWidgetsEntities entities = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                allWidgets= (from w in entities.Widgets.Include("WidgetType").Include("Template") where w.ColumnID==ColumnID
                                                            &&w.Template.ID ==TemplateID
                                                            &&w.WidgetType!=null
                                                            orderby w.OrderNo
                                                            select w).ToList();
            }
            
            foreach(Model.Widget widgetInfo in allWidgets)
            {
                IWidgetContainer widgetContainer = CreateNewWidgetContainer(widgetInfo.ID);
                if (widgetContainer == null)
                {
                    throw new InvalidOperationException(String.Format("Can't load the Container of Widget: {0}", widgetInfo.ID).ToString());
                }
                (widgetContainer as System.Web.UI.Control).ID = Util.GenerateID<IWidgetContainer>(widgetInfo.ID.ToString());
                widgetContainer.IsInEditMode = IsInEditMode;
                widgetContainer.LoadWidget(widgetInfo.ID);
                widgetContainer.Deleted = (wc, w) => {
                    Controls.Remove(wc as System.Web.UI.Control);
                    var parent = Parent;
                    while (!(parent == null || parent is System.Web.UI.UpdatePanel))
                    {
                        parent = parent.Parent;
                    }
                    if (parent is System.Web.UI.UpdatePanel)
                    {
                        (parent as System.Web.UI.UpdatePanel).Update();
                    }
                };
                Controls.Add(widgetContainer as System.Web.UI.Control);
            }
        }

        public IWidgetContainer CreateNewWidgetContainer(long widgetId)
        {
            Model.Widget widgetInfo = null;
            using (Model.CMTWidgetsEntities entities = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                widgetInfo = (from w in entities.Widgets.Include("WidgetContainerType") where w.ID == widgetId select w).FirstOrDefault(); 
            }

            var widgetContainerSrc = String.Empty;
            if (widgetInfo != null && widgetInfo.WidgetContainerType != null)
            {
                widgetContainerSrc = widgetInfo.WidgetContainerType.Src;
            }
            if (!String.IsNullOrEmpty(widgetContainerSrc))
            {
                return Page.LoadControl(widgetContainerSrc) as IWidgetContainer;
            }

            return null;
        }

        public void Refresh()
        {
            foreach (System.Web.UI.Control control in Controls)
            {
                var container = control as IWidgetContainer;
                if (container != null)
                {
                    container.Refresh();
                }
            }
        }

        #endregion
         
        protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Class, Constants.Class_SortableColumn);
            writer.AddAttribute(Constants.Attribute_ColumnID, ID);
            writer.AddAttribute(Constants.Attribute_TemplateID, TemplateID.ToString());
            writer.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
            base.RenderChildren(writer);
            writer.RenderEndTag();
        } 
        
    }
}
